package Mysql

import (
	"database/sql"
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"log"
	"time"
)

var (
	Db  *sql.DB
	err error
)

func init() {
	Db, err = sql.Open("mysql", "root:20030222@tcp(127.0.0.1:3306)/web_db?charset=utf8mb4&parseTime=true&loc=Asia%2FShanghai")
	if err != nil {
		panic(fmt.Sprintf("数据库连接失败:%s", err.Error()))
	}
	Db.SetConnMaxLifetime(3 * time.Minute)
	Db.SetMaxOpenConns(20)
	Db.SetConnMaxIdleTime(20)
	log.Println("数据库初始化完成")
}

func GetDb() *sql.DB {
	return Db
}
